<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

require_once APPPATH . 'controllers/admin.php';

/**
 * Контроллер просмотра и редактирования кинопартнерок
 *
 * @package CoreFramework
 * @version 1.0
 * @author Gennadiy Kozlenko
 */
class Admin_Manage_Partners extends Admin {

    /**
     * Kinoperez feed
     * @var string
     */
    const KINOPEREZ_FEED = 'http://hwdteam.ru/license/kinoperez';

    /**
     * Конструктор класса
     *
     * @access public
     * @return Admin_Manage_Sources
     */
    function Admin_Manage_Partners() {
        parent::Admin();
        $this->set_menu(5, 4);
        $this->load->language('button');
        $this->load->language('video');
        $this->load->helper('http');
    } // end Admin_Manage_Partners

    /**
     * Метод по умолчанию
     *
     * @access public
     */
    function index() {
        $data = array();
        $this->load->helper('link');
        $this->load->model('m_select_partners');
        $this->load->library('table_builder');
        $this->table_builder->set_model($this->m_select_partners);
        $this->table_builder->add_button(create_link($this->get_controller(), 'activate'), $this->lang->line('activate_button'));
        $this->table_builder->add_button(create_link($this->get_controller(), 'pause'), $this->lang->line('pause_button'));
        $this->table_builder->add_button(create_link($this->get_controller(), 'update_positions'), $this->lang->line('update_positions_button'));
        $this->table_builder->add_button(create_link($this->get_controller(), 'edit'), $this->lang->line('edit_button'));
        $data['table'] = $this->table_builder->generate();

        $content = get_content(self::KINOPEREZ_FEED, array(), 5);
        if (!empty($content)) {
            $data['kinoperez'] = (array)json_decode($content);
            foreach ($data['kinoperez'] as $key=>$item) {
                $data['kinoperez'][$key] = (array)$item;
            }
        }

        $this->set_content($this->parser->parse('admin/manage_partners.html', $data, true));
        $this->display();
    } // end index

    /**
     * Метод активации записей
     *
     * @access public
     */
    function activate() {
        $ids = $this->input->post('ids');
        if (is_array($ids) && 0 < count($ids)) {
            $this->load->model('m_partner');
            foreach ($ids as $id) {
                $this->m_partner->set_id($id);
                $this->m_partner->activate();
            }
            $this->set_message($this->lang->line('activate_partners_succ'), 'info', true);
        }
        $this->redirect($this->get_controller());
    } // end activate

    /**
     * Метод приостановки записей
     *
     * @access public
     */
    function pause() {
        $ids = $this->input->post('ids');
        if (is_array($ids) && 0 < count($ids)) {
            $this->load->model('m_partner');
            foreach ($ids as $id) {
                $this->m_partner->set_id($id);
                $this->m_partner->pause();
            }
            $this->set_message($this->lang->line('pause_partners_succ'), 'info', true);
        }
        $this->redirect($this->get_controller());
    } // end pause

    /**
     * Метод изменения записи
     * Редирект на нужный контроллер
     *
     * @access public
     */
    function edit() {
        $ids = $this->input->post('ids');
        if (is_array($ids) && 0 < count($ids)) {
            $id = array_shift($ids);
            $this->redirect('admin_update_partner', '', array('id' => $id));
        }
        $this->redirect($this->get_controller());
    } // end edit

    /**
     * Метод установки позиций
     *
     * @access public
     */
    function update_positions() {
        $positions = $this->input->post('positions');
        if (is_array($positions) && 0 < count($positions)) {
            $this->load->model('m_partner');
            foreach ($positions as $id => $position) {
                $this->m_partner->set_id($id);
                $this->m_partner->set('position', $position);
                $this->m_partner->update();
            }
            $this->set_message($this->lang->line('update_positions_partners_succ'), 'info', true);
        }
        $this->redirect($this->get_controller());
    } // end update_positions

}
